import cv2
import numpy as np
img = cv2.imread(r"/\images\005.jpeg")
# img0 = cv2.cvtColor(img, cv2.COLOR_BGR2GRAY)
# height = img.shape[0]
# width = img.shape[1]
# dsize = (width, height)

kernel = np.ones((5, 5), np.uint8)

#腐蚀操作
# img1 = cv2.erode(img, kernel, iterations=1)
img1 = cv2.morphologyEx(img, op = cv2.MORPH_ERODE, kernel=kernel, iterations=1)

# 膨胀操作
# img2 = cv2.dilate(img, kernel, iterations=5)
img2 = cv2.morphologyEx(img, op = cv2.MORPH_DILATE, kernel=kernel, iterations=1)

# 开运算[腐蚀(erode) + 膨胀(dilate)]
img3 = cv2.morphologyEx(img, op = cv2.MORPH_OPEN, kernel=kernel, iterations=1)

# 闭运算[膨胀(dilate) + 腐蚀(erode)]
img4 = cv2.morphologyEx(img, op = cv2.MORPH_CLOSE, kernel=kernel, iterations=1)

# 形态学梯度运算[膨胀(dilate) - 腐蚀(erode)]
img5 = cv2.morphologyEx(img, op = cv2.MORPH_GRADIENT, kernel=kernel, iterations=1)

# 黑帽运算[闭(close) - 原(origin)]
img6 = cv2.morphologyEx(img, op = cv2.MORPH_BLACKHAT, kernel=kernel, iterations=1)

# 礼帽运算[原(origin) - 开(open)]
img7 = cv2.morphologyEx(img, op = cv2.MORPH_TOPHAT, kernel=kernel, iterations=1)

pic = [("img", img), ("img1", img1), ("img2", img2),
       ("img3", img3), ("img4", img4), ("img5", img5),
       ("img6", img6), ("img7", img7)]

for name, val in pic:
    cv2.imshow(name, val)
    key = cv2.waitKey(5000)
    if key == 27:
        break
cv2.destroyAllWindows()
